package com.ruoyi.education.service.impl;

import java.util.Arrays;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.education.mapper.EduClassMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.education.mapper.EduStudentMapper;
import com.ruoyi.education.domain.EduStudent;
import com.ruoyi.education.service.IEduStudentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 学生信息Service业务层处理
 * 
 * @author Ly
 * @date 2025-06-12
 */
@Service
public class EduStudentServiceImpl implements IEduStudentService 
{
    @Autowired
    private EduStudentMapper eduStudentMapper;

    @Autowired
    private EduClassMapper  eduClassMapper;

    /**
     * 查询学生信息
     * 
     * @param id 学生信息主键
     * @return 学生信息
     */
    @Override
    public EduStudent selectEduStudentById(Long id)
    {
        return eduStudentMapper.selectEduStudentById(id);
    }

    /**
     * 查询学生信息列表
     * 
     * @param eduStudent 学生信息
     * @return 学生信息
     */
    @Override
    public List<EduStudent> selectEduStudentList(EduStudent eduStudent)
    {
        return eduStudentMapper.selectEduStudentList(eduStudent);
    }

    /**
     * 新增学生信息
     * 
     * @param eduStudent 学生信息
     * @return 结果
     */
    @Override
    public int insertEduStudent(EduStudent eduStudent)
    {
        eduStudent.setCreateTime(DateUtils.getNowDate());
        eduStudentMapper.insertEduStudent(eduStudent);
        return eduClassMapper.incrementStudentCount(eduStudent.getClassId());
    }

    /**
     * 修改学生信息
     * 
     * @param eduStudent 学生信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateEduStudent(EduStudent eduStudent)
    {
        //修改学生之前，查询该学生之前的班级信息
        Long classId = eduStudentMapper.selectEduStudentById(eduStudent.getId()).getClassId();
        //和新班级对比
        if (classId != null && !classId.equals(eduStudent.getClassId()))
        {
            //修改班级人数，原班级-1，新班级+1
            eduClassMapper.decrementStudentCount(classId);
            eduClassMapper.incrementStudentCount(eduStudent.getClassId());

        }
        eduStudent.setUpdateTime(DateUtils.getNowDate());
        return  eduStudentMapper.updateEduStudent(eduStudent);
    }

    /**
     * 批量删除学生信息
     * 
     * @param ids 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteEduStudentByIds(Long[] ids)
    {

        //修改学生的班级人数
        Arrays.stream(ids).forEach(id -> {
            //id是学生的id，根据id获取学生班级 id
            EduStudent eduStudent = eduStudentMapper.selectEduStudentById(id);
            eduClassMapper.decrementStudentCount(eduStudent.getClassId());
        });
        //删除学生信息
        eduStudentMapper.deleteEduStudentByIds(ids);

        return 1;
    }

    /**
     * 删除学生信息信息
     * 
     * @param id 学生信息主键
     * @return 结果
     */
    @Override
    public int deleteEduStudentById(Long id)
    {
        return eduStudentMapper.deleteEduStudentById(id);
    }
}
